import Vue from "vue";
import cookie from "../utils/cookies";
import {
  setStore,
  getStore,
  removeStore,
  setSession,
  getSession
} from "../utils/storage";
import { absoluteUrlToBase64 } from "../utils/file.js";

export default {
  //设置语言
  SET_LANG(state, locale) {
    if (state.locales.filter(i => i.code == locale).length > 0) {
      state.locale = locale;
      if (process.client) {
        window.$nuxt.$i18n.locale = locale;
      }
    }
  },
  //设置用户自定义语言开关
  SET_IS_CLIENT_LOCALE(state, boolean) {
    state.is_client_locale = boolean;
  },
  //设置是否为移动设备
  SET_IS_MOBILE(state, boolean) {
    state.is_mobile = boolean;
  },
  //设置是否为IOS设备
  SET_IS_IOS(state, boolean) {
    state.is_ios = boolean;
  },
  //设置是否在微信小程序中
  SET_IS_MINIGRAM(state, boolean) {
    state.is_in_minigram = boolean;
  },
  //设置是否在微信环境中
  SET_IS_WECHAT(state, boolean) {
    state.is_in_wechat = boolean;
  },
  //设置loading
  SET_IS_LOADING(state, boolean) {
    state.is_loading = boolean;
    if (boolean && process.client) {
      window.$nuxt.$store.dispatch("setLoadingAsync");
    }
  },
  //网站基础信息
  SET_WEB_INFO(state, obj) {
    state.web_info = obj;
    if (process.client) {
      document.body.style.setProperty("--theme-color", obj.THEME_COLOR);
    }
  },
  //更新网站基础信息
  UPDATE_WEB_INFO(state, obj) {
    state.web_info = { ...state.web_info, ...obj };
    if (process.client) {
      if (obj.THEME_COLOR) {
        document.body.style.setProperty("--theme-color", obj.THEME_COLOR);
      }
    }
  },
  //在app环境下检测微信是否安装
  UPDATE_IS_INSTALLED_WECHAT(state, boolean) {
    state.is_installed_wechat = boolean;
  },
  //在app环境下检测支付宝是否安装
  UPDATE_IS_INSTALLED_ALIPAY(state, boolean) {
    state.is_installed_alipay = boolean;
  },
  //通知
  SET_NOTICE_LIST(state, arr) {
    state.notice_list = arr;
  },
  //banner
  SET_BANNER_LIST(state, arr) {
    state.banner_list = arr;
  },
  //课程
  SET_COURSE_LIST(state, arr) {
    state.course_list = arr;
  },
  //用户课程
  SET_BUYRECORD_LIST(state, arr) {
    state.buy_record_list = arr;
  },
  //课程套餐
  SET_BUYCOMBO_LIST(state, arr) {
    state.buy_combo_list = arr;
  },
  //课程协议
  SET_SIGN_AGREEMENT_LIST(state, arr) {
    state.sign_Agreement_list = arr;
  },
  //课程签到
  SET_SIGN_IN_COURSE_LIST(state, arr) {
    state.sign_in_course_list = arr;
  },
  //课程购买详细列表
  SET_COURSE_RECORD_LIST(state, arr) {
    state.course_record_list = arr;
  },
  SET_FIREND_LINK_LIST(state, arr) {
    state.friend_links_list = arr;
  },
  //更新购买课程
  UPDATE_BUYRECORD_LIST(state, list) {
    let list_type = Object.prototype.toString.call(list);
    if (list_type == "[object Object]") {
      state.buy_record_list = [list, ...state.buy_record_list];
    } else if (list_type == "[object Array]") {
      state.buy_record_list = [...list, ...state.buy_record_list];
    }
  },
  //更新购买套餐
  UPDATE_BUYCOMBO_LIST(state, list) {
    let list_type = Object.prototype.toString.call(list);
    if (list_type == "[object Object]") {
      state.buy_combo_list = [list, ...state.buy_combo_list];
    } else if (list_type == "[object Array]") {
      state.buy_combo_list = [...list, ...state.buy_combo_list];
    }
  },
  //更新课程协议
  UPDATE_SIGN_AGREEMENT_LIST(state, list) {
    let list_type = Object.prototype.toString.call(list);
    if (list_type == "[object Object]") {
      state.sign_Agreement_list = [list, ...state.sign_Agreement_list];
    } else if (list_type == "[object Array]") {
      state.sign_Agreement_list = [...list, ...state.sign_Agreement_list];
    }
  },
  //更新当天签到课程
  UPDATE_SIGN_IN_COURSE_LIST(state, list) {
    let list_type = Object.prototype.toString.call(list);
    if (list_type == "[object Object]") {
      state.sign_in_course_list = [list, ...state.sign_in_course_list];
    } else if (list_type == "[object Array]") {
      state.sign_in_course_list = [...list, ...state.sign_in_course_list];
    }
  },
  //初始用户信息
  INIT_USER: state => {
    let token = cookie.getInClient("token");
    if (token) {
      let user_info = JSON.parse(getStore("user_info"));
      let login_type = cookie.getInClient("login_type");
      let user_type = cookie.getInClient("user_type");
      state.user_type = user_type;
      state.token = token;
      state.login_type = login_type;
      state.user_info = { ...user_info };
      if (!user_info) {
        state.user_type = "";
        state.token = "";
        state.login_type = "";
        state.user_info = {};
        cookie.delInClient("token");
        cookie.delInClient("login_type");
        cookie.delInClient("user_type");
        removeStore("user_info");
      }
    } else {
      state.user_info = {};
      removeStore("user_info");
    }
  },
  //token
  SET_TOKEN(state, string, boolean) {
    state.token = string;
    cookie.setInClient(
      { key: "token", val: string },
      boolean ? 3 * 24 * 60 * 60 : 24 * 60 * 60
    );
  },
  //设置登录类型（学生/家长）
  SET_USERTYPE(state, string, boolean) {
    state.user_type = string;
    cookie.setInClient(
      { key: "user_type", val: string },
      boolean ? 3 * 24 * 60 * 60 : 24 * 60 * 60
    );
  },
  //设置用户信息
  async SET_USERINFO(state, obj) {
    let user_info = { ...obj };
    state.user_info = user_info;
    const { gender, avatar } = obj;
    const img01 = await absoluteUrlToBase64(
      require("../static/img/pc/avatar05.png")
    );
    const img02 = await absoluteUrlToBase64(
      require("../static/img/pc/avatar07.png")
    );
    const img03 = await absoluteUrlToBase64(
      require("../static/img/pc/avatar08.png")
    );
    user_info = {
      ...obj,
      avatar: avatar
        ? avatar
        : gender === 0
        ? img01.dataURL
        : gender === 1
        ? img02.dataURL
        : img03.dataURL
    };
    // console.log("user_info:", user_info);
    state.user_info = user_info;
    setStore("user_info", user_info);
  },
  //更新用户信息
  async UPDATE_USERINFO(state, obj) {
    let user_info = {
      ...state.user_info,
      ...obj
    };
    const { gender, avatar } = user_info;
    const img01 = await absoluteUrlToBase64(
      require("../static/img/pc/avatar05.png")
    );
    const img02 = await absoluteUrlToBase64(
      require("../static/img/pc/avatar07.png")
    );
    const img03 = await absoluteUrlToBase64(
      require("../static/img/pc/avatar08.png")
    );
    user_info = {
      ...user_info,
      avatar: avatar
        ? avatar
        : gender === 0
        ? img01.dataURL
        : gender === 1
        ? img02.dataURL
        : img03.dataURL
    };
    state.user_info = user_info;
    removeStore("user_info");
    setStore("user_info", user_info);
  },
  //设置用户认证信息
  async SET_AUTHENTICATION(state, obj) {
    state.authentication = { ...obj };
  },
  //更新用户认证信息
  async UPDATE_AUTHENTICATION(state, obj) {
    state.authentication = { ...state.authentication, ...obj };
  },
  //登陆类型
  SET_LOGINTYPE(state, string) {
    cookie.setInClient({ key: "login_type", val: string });
    state.login_type = string;
  },
  // 退出登录
  SIGN_OUT: state => {
    state.token = "";
    state.login_type = "";
    state.user_info = {};
    state.user_type = "";

    state.member_discount = {};
    state.member_info = {};
    state.authentication = {
      departmentId: null,
      hospitalId: null,
      identityId: null,
      professionalId: null
    };
    state.course_record_list = [];
    state.buy_record_list = [];
    cookie.delInClient("token");
    cookie.delInClient("login_type");
    cookie.delInClient("user_type");
    removeStore("user_info");
  },

  GET_TOKEN_SERVER: (state, req) => {
    let cook = cookie.getInServer(req);
    state.token = cook["token"];
  },
  //专业列表
  SET_MAJOR_LIST: (state, arr) => {
    state.major_list = arr;
  },
  //专业树
  SET_MAJOR_TREE: (state, arr) => {
    state.major_tree = arr;
  },
  //班级列表
  SET_CLASS_LIST: (state, arr) => {
    state.class_list = arr;
  },
  //手机导航列表
  SET_NAV_list: (state, arr) => {
    state.nav_list = arr;
  },
  //初始历史搜索记录
  INIT_SEARCH_HISTORY: (state, arr) => {
    let search_history = JSON.parse(getStore("search_history"));
    if (search_history && search_history.length > 0) {
      state.search_history_list = search_history;
    } else {
      state.search_history_list = [];
    }
  },
  //设置历史搜索记录
  SET_SEARCH_HISTORY: (state, str) => {
    if (!state.search_history_list.includes(str)) {
      state.search_history_list = [str, ...state.search_history_list];
      setStore("search_history", [...state.search_history_list]);
    }
  },
  //清除历史搜索记录
  CLEAR_SEARCH_HISTORY: (state, str) => {
    state.search_history_list = [];
    removeStore("search_history");
  },
  //初始题库历史搜索记录
  INIT_QUESTION_BANK_SEARCH_HISTORY: (state, arr) => {
    let search_history = JSON.parse(getStore("questionBank_search_history"));
    if (search_history && search_history.length > 0) {
      state.questionBank.search_history_list = search_history;
    } else {
      state.questionBank.search_history_list = [];
    }
  },
  //设置题库历史搜索记录
  SET_QUESTION_BANK_SEARCH_HISTORY: (state, str) => {
    if (!state.search_history_list.includes(str)) {
      state.questionBank.search_history_list = [
        str,
        ...state.questionBank.search_history_list
      ];
      setStore("questionBank_search_history", [
        ...state.questionBank.search_history_list
      ]);
    }
  },
  //清除题库历史搜索记录
  CLEAR_QUESTION_BANK_SEARCH_HISTORY: (state, str) => {
    state.questionBank.search_history_list = [];
    removeStore("questionBank_search_history");
  },
  //设置题库购买列表
  SET_QUESTION_LIST: (state, arr) => {
    state.questionBank.question_record_list = arr;
  },
  //更新题库购买列表
  UPDATE_QUESTION_LIST: (state, obj) => {
    state.questionBank.question_record_list = [
      ...state.questionBank.question_record_list,
      obj
    ];
  },
  SETMD: (state, obj) => {
    state.kl_md = {
      ...state.kl_md,
      ...obj
    };
  },
  //设置第三方登录相关
  SET_THIRD_PART_INFO: (state, obj) => {
    state.third_part_login = obj;
  },
  //设置会员优惠信息
  SET_MEMBER_DISCOUNT: (state, obj) => {
    state.member_discount = {
      ...obj,
      majorIds: JSON.parse(obj.majorIds),
      courseIds: JSON.parse(obj.courseIds),
      questionGroupIds: JSON.parse(obj.questionGroupIds),
    };
  },
  //设置会员信息
  SET_MEMBER_INFO: (state, obj) => {
    state.member_info = obj;
  },
  //设置rtc相关
  SET_RTC_INFO: (state, obj) => {
    state.rtc_info = obj;
  },
  //设置主题色更浅的颜色
  SET_THEME_COLOR_LIGHTER: (state, color) => {
    state.THEME_COLOR_LIGHTER = color;
    document.body.style.setProperty("--theme-color-lighter", color);
  },
  //设置主题色更深的颜色
  SET_THEME_COLOR_DARKER: (state, color) => {
    state.THEME_COLOR_DARKER = color;
    document.body.style.setProperty("--theme-color-darker", color);
  },
  SET_V2QUESTION_ROUTER: (state, arr) => {
    if (process.client) {
      window.$nuxt._router.options.routes.forEach((item, index) => {
        if (item.path == "/m") {
          window.$nuxt._router.options.routes.splice(index, 1, {
            ...item,
            redirect: "/m/v2-question-bank"
          });
        }
      });
      if (window.$nuxt.$route.path == "/m/index") {
        window.$nuxt.$router.replace("/m/v2-question-bank");
      }
      window.$nuxt.$store.commit("SET_IS_LOADING", false);
    }
  }
};
